﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Data;
using Workflow.Alerts.DAL;
using Workflow.Alerts.Interfaces.ProfileSettings;

namespace Workflow.Alerts
{
    public class MaterialsListPresenter : Presenter
    {
        #region members
        private readonly MaterialsViewDAL model;
        private readonly IMaterialsListView view;
        #endregion

        #region constructor
        public MaterialsListPresenter(IMaterialsListView _view)
            : this(_view, null)
        { }

        public MaterialsListPresenter(IMaterialsListView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IMaterialsListView>();
            view.OnPageLoad += view_OnLoad;
            model = new MaterialsViewDAL();
        }

        #endregion

        #region private methods

        private void view_OnLoad()
        {            
            var filter = new WorkflowFilter
            {
                MunicipalityId = CurrentUser.MunicipalityId
            };

            var filterSql = new WorkflowFilterSql(filter);

            EstateDataSet.MaterialsDataTable materials = model.GetMaterialsListByMunicipality(filterSql);
            foreach (EstateDataSet.MaterialsRow material in materials)
            {
                material.unitname = WorkflowEnums.MaterialsUnits[material.unit];
            }
            materials.AcceptChanges();

            view.Materials = materials;
        }

        #endregion
    }
}
